package java2022spring;

import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;

import javax.swing.*;
import java.awt.*;
import java.util.regex.Pattern;

public class Window extends JFrame {
    private static final int Window_Width=500;
    private static final int Window_Height=750;
    private GridBagConstraints constraints_panel,constraints_window;//网格包约束条件
    private GridBagLayout bagLayout;//网格包布局
    private JTextArea text,text_history;//文本域
    private JScrollPane jsHistory;
    private JPanel pNorth,pCenter;//面板
    private char option=' ';//保存符号
    private double value=0;//保存值
    private boolean result=false;//判断是否为结果
    private boolean connectnNum=true;//连接字符
    private boolean go=true;//判断是否进行计算
    //按钮
    private JButton but1,but2,but3,but4,but5,but6,but7,but8,but9,but0,
            butPlus,butSubtract,butMultiply,butExcept,butAmount,butPoint,
            butOpposite,butClear,butBack,butPer,
            butRoot,butPow,butLog,
            butSin,butCos,butTan,butExp,butFraction,
            butPI,butJuedui;

    JDialog dialog;//历史记录窗口

    private Image title;//软件图标

    //主题选项和类型选项
    private JMenuBar menu;
    private JMenu theme,type;
    private JMenuItem theme_Dark,theme_Color,theme_Simple,type_Normal,type_Scientific,history,
            //空项目（为历史记录腾空间）
            empty01,empty02;

    public Window(){
        //初始化
        super("计算器");
        pNorth=new JPanel();
        pCenter=new JPanel();
        bagLayout=new GridBagLayout();
        setLayout(bagLayout);
        constraints_panel =new GridBagConstraints();
        constraints_panel.fill=GridBagConstraints.BOTH;
        constraints_panel.weightx=0.5;
        constraints_panel.weighty=0.3;
        constraints_window=new GridBagConstraints();
        constraints_window.fill=GridBagConstraints.BOTH;
        constraints_window.weightx=0.5;
        constraints_window.weighty=0.3;

        //设置图标
        title=Toolkit.getDefaultToolkit().createImage(Main.class.getResource("Images/title.png"));
        setIconImage(title);

        //菜单添加
        menu=new JMenuBar();
        theme=new JMenu("主题");
        theme.setCursor(new Cursor(Cursor.HAND_CURSOR));
        type=new JMenu("模式");
        type.setCursor(new Cursor(Cursor.HAND_CURSOR));
        theme_Simple=new JMenuItem("简单主题");
        theme_Simple.setCursor(new Cursor(Cursor.HAND_CURSOR));
        theme_Simple.addActionListener(e -> {
            text.setBackground(Color.WHITE);
            text.setForeground(Color.BLACK);
            pCenter.setBackground(Color.WHITE);
            pNorth.setBackground(Color.WHITE);
            butRoot.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butLog.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butPow.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butAmount.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butClear.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butBack.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butPer.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butExcept.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butPlus.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butMultiply.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butSubtract.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but0.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but1.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but2.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but3.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but4.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but5.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but6.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but7.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but8.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but9.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butPoint.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butOpposite.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butSin.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butCos.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butTan.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butExp.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butFraction.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butPI.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butJuedui.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
        });
        theme_Dark=new JMenuItem("黑色主题");
        theme_Dark.setCursor(new Cursor(Cursor.HAND_CURSOR));
        theme_Dark.addActionListener(e -> {
            text.setBackground(Color.GRAY);
            text.setForeground(Color.white);
            pNorth.setBackground(Color.GRAY);
            pCenter.setBackground(Color.GRAY);
            but0.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark));
            but1.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark));
            but2.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark));
            but3.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark));
            but4.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark));
            but5.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark));
            but6.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark));
            but7.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark));
            but8.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark));
            but9.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark));
            butPoint.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark));
            butOpposite.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark));
            butAmount.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butPlus.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butSubtract.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butLog.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butPow.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butMultiply.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butRoot.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butBack.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butPer.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butClear.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butExcept.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butSin.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butCos.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butTan.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butExp.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butFraction.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butPI.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butJuedui.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
        });
        theme_Color=new JMenuItem("彩色主题");
        theme_Color.setCursor(new Cursor(Cursor.HAND_CURSOR));
        theme_Color.addActionListener(e -> {
            text.setBackground(Color.WHITE);
            text.setForeground(Color.BLACK);
            pCenter.setBackground(Color.WHITE);
            pNorth.setBackground(Color.WHITE);
            butClear.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.red));
            butBack.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.green));
            butPer.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.blue));
            butExcept.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butMultiply.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butPlus.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            butSubtract.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.primaryDark));
            but0.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but1.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but2.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but3.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but4.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but5.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but6.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but7.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but8.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            but9.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butPoint.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butOpposite.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.normal));
            butAmount.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue));
            butPow.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.green));
            butLog.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.blue));
            butRoot.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.red));
            butSin.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark));
            butCos.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark));
            butTan.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark));
            butFraction.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.red));
            butExp.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.secondaryDark));
            butPI.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.green));
            butJuedui.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.blue));
        });
        type_Normal=new JMenuItem("标准模式");
        type_Normal.setCursor(new Cursor(Cursor.HAND_CURSOR));
        type_Normal.addActionListener(e -> {
            setSize(Window_Width,Window_Height);
            butRoot.setVisible(false);
            butPow.setVisible(false);
            butLog.setVisible(false);
            butSin.setVisible(false);
            butCos.setVisible(false);
            butTan.setVisible(false);
            butExp.setVisible(false);
            butFraction.setVisible(false);
            butPI.setVisible(false);
            butJuedui.setVisible(false);
        });
        type_Scientific=new JMenuItem("科学模式");
        type_Scientific.setCursor(new Cursor(Cursor.HAND_CURSOR));
        type_Scientific.addActionListener(e -> {
            setSize(Window_Width+120,Window_Height+80);
            butRoot.setVisible(true);
            butLog.setVisible(true);
            butPow.setVisible(true);
            butSin.setVisible(true);
            butCos.setVisible(true);
            butTan.setVisible(true);
            butExp.setVisible(true);
            butFraction.setVisible(true);
            butPI.setVisible(true);
            butJuedui.setVisible(true);
        });
        history=new JMenuItem("历史记录");
        history.setCursor(new Cursor(Cursor.HAND_CURSOR));
        history.addActionListener(e -> {
            showDialog();
        });
        empty01=new JMenuItem();
        empty02=new JMenuItem();
        theme.add(theme_Simple);
        theme.add(theme_Color);
        theme.add(theme_Dark);
        menu.add(theme);
        type.add(type_Scientific);
        type.add(type_Normal);
        menu.add(type);
        menu.add(history);
        menu.add(empty01);
        menu.add(empty02);

        //设置文本框
        text=new JTextArea("0");
        text.setLineWrap(true);//自动换行
        text.setWrapStyleWord(true);//断行不断字
        text.setFont(new Font("Comic Sans Ms",Font.BOLD+Font.ITALIC,40));
        text.setSize(500,250);
        text.setEditable(false);
        text.setFocusable(false);//取消焦点
        text.setBackground(Color.WHITE);
        text.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        text_history=new JTextArea("");
        text_history.setWrapStyleWord(true);//断行不断字
        text_history.setFont(new Font("Comic Sans Ms",Font.BOLD+Font.ITALIC,40));
        text_history.setEditable(false);
        text_history.setFocusable(false);//取消焦点
        text_history.setBackground(Color.WHITE);
        text_history.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        jsHistory=new JScrollPane(text_history);
        jsHistory.setSize(750,500);
        jsHistory.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        //设置按钮
        //“C”清除符
        butClear=new Button("C",e -> {
            repaintFont();
            text.setText("0");
            option=' ';
            value=0;
            if(Pattern.matches("[0]*",text.getText()))
                if(!result)
                    text_history.setText("");
            result=false;
        });
        //"<-"退格符
        butBack=new Button("<-",e -> {
            repaintFont();
            String str =text.getText();
            StringBuffer strB=new StringBuffer();
            for(int i=0;i<(str.length()-1);i++)
                strB.append(str.charAt(i));
            if(!result) {
                if (strB.toString().equals(""))
                    text.setText("0");
                else
                    text.setText(strB.toString());
            }
        });
        //"%"取余符
        butPer=new Button("%",e -> {
            repaintFont();
            if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){
                if(go){
                    value=calculate(value,text.getText(),option);
                    if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){
                        text.setText(String.valueOf((int)value));
                    }
                    else{
                        text.setText(String.valueOf(value));
                    }
                    option='%';
                    connectnNum=false;
                    go=false;
                }
            }
        });
        //"/"除号
        butExcept=new Button("/",e -> {
            repaintFont();
            if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){
                if(go){
                    value=calculate(value,text.getText(),option);
                    if(value==(Double.valueOf(text.getText())))
                        text_history.append(""+value+"/");
                    else
                        text_history.append(text.getText()+"/");
                    if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){
                        text.setText(String.valueOf((int)value));
                    }
                    else {
                        text.setText(String.valueOf(value));
                    }
                    option='/';
                    connectnNum=false;
                    go=false;
                }
                else {
                    option='/';
                }
            }
        });
        //"√"根号
        butRoot=new Button("√",e -> {
            repaintFont();
            if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){
                if(go){
                    value=calculate(value,text.getText(),option);
                    if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){
                        text.setText(String.valueOf((int)value));
                    }
                    else {
                        text.setText(String.valueOf(value));
                    }
                    option='√';
                    connectnNum=false;
                    go=false;
                }
                else {
                    option='√';
                }
            }
        });
        butRoot.setVisible(false);
        //数字"7"
        but7=new Button("7",e -> {
            repaintFont();
            if(connectnNum){
                if(Pattern.matches("[0]*",text.getText())){
                    text.setText("7");
                }
                else{
                    text.setText(text.getText()+"7");
                }
            }
            else{
                text.setText("7");
                connectnNum=true;
            }
            go=true;
        });
        //数字"8"
        but8=new Button("8",e -> {
            repaintFont();
            if(connectnNum){
                if(Pattern.matches("[0]*",text.getText())){
                    text.setText("8");
                }
                else{
                    text.setText(text.getText()+"8");
                }
            }
            else{
                text.setText("8");
                connectnNum=true;
            }
            go=true;
        });
        //数字”9“
        but9=new Button("9",e -> {
            repaintFont();
            if(connectnNum){
                if(Pattern.matches("[0]*",text.getText())){
                    text.setText("9");
                }
                else{
                    text.setText(text.getText()+"9");
                }
            }
            else{
                text.setText("9");
                connectnNum=true;
            }
            go=true;
        });
        //"*"乘号
        butMultiply=new Button("*",e -> {
            repaintFont();
            if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){
                if(go){
                    value=calculate(value,text.getText(),option);
                    if(value==(Double.valueOf(text.getText())))
                        text_history.append(""+value+"*");
                    else
                        text_history.append(text.getText()+"*");
                    if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){
                        text.setText(String.valueOf((int)value));
                    }
                    else {
                        text.setText(String.valueOf(value));
                    }
                    option='*';
                    connectnNum=false;
                    go=false;
                }
                else {
                    option='*';
                }
            }
        });
        //"^"次幂
        butPow=new Button("^",e -> {
            repaintFont();
            if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){
                if(go){
                    value=calculate(value,text.getText(),option);
                    if(value==(Double.valueOf(text.getText())))
                        text_history.append(""+value+"^");
                    else
                        text_history.append(text.getText()+"^");
                    if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){
                        text.setText(String.valueOf((int)value));
                    }
                    else {
                        text.setText(String.valueOf(value));
                    }
                    option='^';
                    connectnNum=false;
                    go=false;
                }
                else {
                    option='^';
                }
            }
        });
        butPow.setVisible(false);
        //数字"4"
        but4=new Button("4",e -> {
            repaintFont();
            if(connectnNum){
                if(Pattern.matches("[0]*",text.getText())){
                    text.setText("4");
                }
                else{
                    text.setText(text.getText()+"4");
                }
            }
            else{
                text.setText("4");
                connectnNum=true;
            }
            go=true;
        });
        //数字"5"
        but5=new Button("5",e -> {
            repaintFont();
            if(connectnNum){
                if(Pattern.matches("[0]*",text.getText())){
                    text.setText("5");
                }
                else{
                    text.setText(text.getText()+"5");
                }
            }
            else{
                text.setText("5");
                connectnNum=true;
            }
            go=true;
        });
        //数字”6“
        but6=new Button("6",e -> {
            repaintFont();
            if(connectnNum){
                if(Pattern.matches("[0]*",text.getText())){
                    text.setText("6");
                }
                else{
                    text.setText(text.getText()+"6");
                }
            }
            else{
                text.setText("6");
                connectnNum=true;
            }
            go=true;
        });
        //"-"减号
        butSubtract=new Button("-",e -> {
            repaintFont();
            if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){
                if(go){
                    value=calculate(value,text.getText(),option);
                    if(value==(Double.valueOf(text.getText())))
                        text_history.append(""+value+"-");
                    else
                        text_history.append(text.getText()+"-");
                    if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){
                        text.setText(String.valueOf((int)value));
                    }
                    else {
                        text.setText(String.valueOf(value));
                    }
                    option='-';
                    connectnNum=false;
                    go=false;
                }
                else {
                    option='-';
                }
            }
        });
        //"ln"对数
        butLog=new Button("ln",e -> {
            repaintFont();
            if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){
                if(go){
                    value=calculate(value,text.getText(),option);
                    if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){
                        text.setText(String.valueOf((int)value));
                    }
                    else {
                        text.setText(String.valueOf(value));
                    }
                    option='l';
                    connectnNum=false;
                    go=false;
                }
                else {
                    option='l';
                }
            }
        });
        butLog.setVisible(false);
        //数字”1“
        but1=new Button("1",e -> {
            repaintFont();
            if(connectnNum){
                if(Pattern.matches("[0]*",text.getText())){
                    text.setText("1");
                }
                else{
                    text.setText(text.getText()+"1");
                }
            }
            else{
                text.setText("1");
                connectnNum=true;
            }
            go=true;
        });
        //数字”2“
        but2=new Button("2",e -> {
            repaintFont();
            if(connectnNum){
                if(Pattern.matches("[0]*",text.getText())){
                    text.setText("2");
                }
                else{
                    text.setText(text.getText()+"2");
                }
            }
            else{
                text.setText("2");
                connectnNum=true;
            }
            go=true;
        });
        //数字”3“
        but3=new Button("3",e -> {
            repaintFont();
            if(connectnNum){
                if(Pattern.matches("[0]*",text.getText())){
                    text.setText("3");
                }
                else{
                    text.setText(text.getText()+"3");
                }
            }
            else{
                text.setText("3");
                connectnNum=true;
            }
            go=true;
        });
        //"+"加号
        butPlus=new Button("+",e -> {
            repaintFont();
            if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){
                if(go){
                    value=calculate(value,text.getText(),option);
                    if(value==(Double.valueOf(text.getText())))
                        text_history.append(""+value+"+");
                    else
                        text_history.append(text.getText()+"+");
                    if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){
                        text.setText(String.valueOf((int)value));
                    }
                    else {
                        text.setText(String.valueOf(value));
                    }
                    option='+';
                    connectnNum=false;
                    go=false;
                }
                else {
                    option='+';
                }
            }
        });
        //"+/-"负号
        butOpposite=new Button("+/-",e -> {
            repaintFont();
            if(connectnNum){
                if(Pattern.matches("[0]*",text.getText())){
                    text.setText("0");
                }
                else{
                    text.setText(""+(Double.parseDouble(text.getText())*(-1)));
                }
            }
            else{
                text.setText(""+(Double.parseDouble(text.getText())*(-1)));
            }
            go=true;
        });
        //数字”0“
        but0=new Button("0",e -> {
            repaintFont();
            if(connectnNum){
                if(Pattern.matches("[0]*",text.getText())){
                    text.setText("0");
                }
                else{
                    text.setText(text.getText()+"0");
                }
            }
            else{
                text.setText("0");
                connectnNum=true;
            }
            go=true;
        });
        //"."小数点
        butPoint=new Button(".",e -> {
            repaintFont();
            if(connectnNum){
                if(!text.getText().contains(".")){
                    text.setText(text.getText()+".");
                }
            }
            else{
                text.setText("0.");
                connectnNum=true;
            }
            go=true;
        });
        //"="等于号
        butAmount=new Button("=",e -> {
            if(Pattern.matches("([-]?\\d+[.]\\d*)|([-]?\\d+)",text.getText())) {
                if (go|option=='l'|option=='√'|option=='S'|option=='C'|option=='T'|option=='e'|option=='F'|option=='J') {
                    if(option=='l'){
                        text_history.append("ln"+value);
                    }
                    else if(option=='√'){
                        text_history.append("√"+value);
                    }
                    else if(option=='S'){
                        text_history.append("sin"+value);
                    }
                    else if(option=='C'){
                        text_history.append("cos"+value);
                    }
                    else if(option=='T'){
                        text_history.append("tan"+value);
                    }
                    else if(option=='e'){
                        text_history.append("e^"+value);
                    }
                    else if(option=='F'){
                        text_history.append("1/"+value);
                    }
                    else if(option=='J'){
                        text_history.append("|"+value+"|");
                    }
                    value = calculate(value, text.getText(), option);
                    if(option=='l'|option=='√'|option=='S'|option=='C'|option=='T'|option=='e'|option=='F'|option=='J')
                        text_history.append("="+value+"\n");
                    else
                        text_history.append(""+text.getText()+"="+value+"\n");
                    text.setText(String.valueOf(value));
                    option = '=';
                    connectnNum = false;
                    result = true;

                }
            }
        });
        //"sin"正弦函数
        butSin=new Button("Sin",e -> {
            repaintFont();
            if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){
                if(go){
                    value=calculate(value,text.getText(),option);
                    if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){
                        text.setText(String.valueOf((int)value));
                    }
                    else {
                        text.setText(String.valueOf(value));
                    }
                    option='S';
                    connectnNum=false;
                    go=false;
                }
                else {
                    option='S';
                }
            }
        });
        butSin.setVisible(false);
        //"cos"余弦函数
        butCos=new Button("Cos",e -> {
            repaintFont();
            if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){
                if(go){
                    value=calculate(value,text.getText(),option);
                    if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){
                        text.setText(String.valueOf((int)value));
                    }
                    else {
                        text.setText(String.valueOf(value));
                    }
                    option='C';
                    connectnNum=false;
                    go=false;
                }
                else {
                    option='C';
                }
            }
        });
        butCos.setVisible(false);
        //"tan"正切函数
        butTan=new Button("Tan",e -> {
            repaintFont();
            if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){
                if(go){
                    value=calculate(value,text.getText(),option);
                    if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){
                        text.setText(String.valueOf((int)value));
                    }
                    else {
                        text.setText(String.valueOf(value));
                    }
                    option='T';
                    connectnNum=false;
                    go=false;
                }
                else {
                    option='T';
                }
            }
        });
        butTan.setVisible(false);
        //"e^x"对数
        butExp=new Button("e^x",e -> {
            repaintFont();
            if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){
                if(go){
                    value=calculate(value,text.getText(),option);
                    if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){
                        text.setText(String.valueOf((int)value));
                    }
                    else {
                        text.setText(String.valueOf(value));
                    }
                    option='e';
                    connectnNum=false;
                    go=false;
                }
                else {
                    option='e';
                }
            }
        });
        butExp.setVisible(false);
        //"1/x"倒数
        butFraction=new Button("1/x",e -> {
            repaintFont();
            if(Pattern.matches("([-]?\\d+[.]\\d*)|(\\d+)",text.getText())){
                if(go){
                    value=calculate(value,text.getText(),option);
                    if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){
                        text.setText(String.valueOf((int)value));
                    }
                    else {
                        text.setText(String.valueOf(value));
                    }
                    option='F';
                    connectnNum=false;
                    go=false;
                }
                else {
                    option='F';
                }
            }
        });
        butFraction.setVisible(false);
        //"π"常数π
        butPI=new Button("π",e -> {
            repaintFont();
            if(connectnNum){
                if(Pattern.matches("[0]*",text.getText())){
                    text.setText(String.valueOf(Math.PI));
                    connectnNum=false;
                }
            }
            else{
                text.setText(String.valueOf(Math.PI));
            }
            go=true;
        });
        butPI.setVisible(false);
        //"|x|"绝对值
        butJuedui=new Button("|x|",e -> {
            repaintFont();
            if(Pattern.matches("([-]?\\d+[.]\\d*)|([-]?\\d+)",text.getText())){
                if(go){
                    value=calculate(value,text.getText(),option);
                    if(Pattern.matches("[-]?[\\d]+[.][0]*",String.valueOf(value))){
                        text.setText(String.valueOf((int)value));
                    }
                    else {
                        text.setText(String.valueOf(value));
                    }
                    option='J';
                    connectnNum=false;
                    go=false;
                }
                else {
                    option='J';
                }
            }
        });
        butJuedui.setVisible(false);

        //菜单与文本框的面板设置
        pNorth.setLayout(new BorderLayout());
        pNorth.add(menu,BorderLayout.NORTH);
        pNorth.add(text,BorderLayout.CENTER);

        //按钮面板设置
        //第一行
        pCenter.setLayout(bagLayout);
        constraints_panel.gridwidth=1;
        bagLayout.setConstraints(butRoot, constraints_panel);
        pCenter.add(butRoot);
        constraints_panel.gridx=1;
        bagLayout.setConstraints(butPow, constraints_panel);
        pCenter.add(butPow);
        constraints_panel.gridx=2;
        bagLayout.setConstraints(butPI,constraints_panel);
        pCenter.add(butPI);
        constraints_panel.gridx=3;
        bagLayout.setConstraints(butJuedui,constraints_panel);
        pCenter.add(butJuedui);
        constraints_panel.gridx=4;
        bagLayout.setConstraints(butLog, constraints_panel);
        pCenter.add(butLog);
        //第二行
        constraints_panel.gridwidth=1;
        constraints_panel.gridx=0;
        bagLayout.setConstraints(butFraction,constraints_panel);
        pCenter.add(butFraction);
        constraints_panel.gridx=1;
        bagLayout.setConstraints(butClear, constraints_panel);
        pCenter.add(butClear);
        constraints_panel.gridx=2;
        bagLayout.setConstraints(butBack, constraints_panel);
        pCenter.add(butBack);
        constraints_panel.gridx=3;
        bagLayout.setConstraints(butPer, constraints_panel);
        pCenter.add(butPer);
        constraints_panel.gridx=4;
        bagLayout.setConstraints(butExcept, constraints_panel);
        pCenter.add(butExcept);
        //第三行
        constraints_panel.gridwidth=1;
        constraints_panel.gridx=0;
        bagLayout.setConstraints(butExp, constraints_panel);
        pCenter.add(butExp);
        constraints_panel.gridx=1;
        bagLayout.setConstraints(but7, constraints_panel);
        pCenter.add(but7);
        constraints_panel.gridx=2;
        bagLayout.setConstraints(but8, constraints_panel);
        pCenter.add(but8);
        constraints_panel.gridx=3;
        bagLayout.setConstraints(but9, constraints_panel);
        pCenter.add(but9);
        constraints_panel.gridx=4;
        bagLayout.setConstraints(butMultiply, constraints_panel);
        pCenter.add(butMultiply);
        //第四行
        constraints_panel.gridwidth=1;
        constraints_panel.gridx=0;
        bagLayout.setConstraints(butTan,constraints_panel);
        pCenter.add(butTan);
        constraints_panel.gridx=1;
        bagLayout.setConstraints(but4, constraints_panel);
        pCenter.add(but4);
        constraints_panel.gridx=2;
        bagLayout.setConstraints(but5, constraints_panel);
        pCenter.add(but5);
        constraints_panel.gridx=3;
        bagLayout.setConstraints(but6, constraints_panel);
        pCenter.add(but6);
        constraints_panel.gridx=4;
        bagLayout.setConstraints(butSubtract, constraints_panel);
        pCenter.add(butSubtract);
        //第五行
        constraints_panel.gridwidth=1;
        constraints_panel.gridx=0;
        bagLayout.setConstraints(butCos,constraints_panel);
        pCenter.add(butCos);
        constraints_panel.gridx=1;
        bagLayout.setConstraints(but1,constraints_panel);
        pCenter.add(but1);
        constraints_panel.gridx=2;
        bagLayout.setConstraints(but2,constraints_panel);
        pCenter.add(but2);
        constraints_panel.gridx=3;
        bagLayout.setConstraints(but3,constraints_panel);
        pCenter.add(but3);
        constraints_panel.gridx=4;
        bagLayout.setConstraints(butPlus,constraints_panel);
        pCenter.add(butPlus);
        //第六行
        constraints_panel.gridwidth=1;
        constraints_panel.gridx=0;
        bagLayout.setConstraints(butSin,constraints_panel);
        pCenter.add(butSin);
        constraints_panel.gridx=1;
        bagLayout.setConstraints(butOpposite,constraints_panel);
        pCenter.add(butOpposite);
        constraints_panel.gridx=2;
        bagLayout.setConstraints(but0,constraints_panel);
        pCenter.add(but0);
        constraints_panel.gridx=3;
        bagLayout.setConstraints(butPoint,constraints_panel);
        pCenter.add(butPoint);
        constraints_panel.gridx=4;
        bagLayout.setConstraints(butAmount,constraints_panel);
        pCenter.add(butAmount);

        //窗口布局
        constraints_window.gridwidth=GridBagConstraints.REMAINDER;
        bagLayout.setConstraints(pNorth, constraints_window);
        add(pNorth);
        bagLayout.setConstraints(pCenter, constraints_window);
        add(pCenter);

        //窗口基础设置
        setSize(Window_Width,Window_Height);
        setLocationRelativeTo(null);//window居中显示
        setResizable(false);//不可改变窗口大小
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    //重新绘制字体
    public void repaintFont(){
        text.setFont(new Font("Comic Sans Ms",Font.BOLD+Font.ITALIC,40));
    }

    //计算方法
    public double calculate(double x,String input,char option){
        double y = Double.parseDouble(input);

        text.setFont(new Font("Comic Sans Ms",Font.BOLD+Font.ITALIC,40));

        switch (option){
            case'+':
                return x+y;
            case'-':
                return (x)-(y);
            case'*':
                return x*y;
            case'/':
                return x/y;
            case'%':
                return x%y;
            case'^':
                return Math.pow(x,y);
            case'l':
                return Math.log(x);
            case'√':
                return Math.sqrt(x);
            case'O':
                return x*-1;
            case'S': {
                double value=Math.toRadians(x);
                return Math.sin(value);
            }
            case'C': {
                double value = Math.toRadians(x);
                return Math.cos(value);
            }
            case'T': {
                double value=Math.toRadians(x);
                return Math.tan(value);
            }
            case'e':
                return Math.exp(x);
            case'F':
                return 1/x;
            case'J':{
                if(Pattern.matches("([1-9]\\d*.\\d*|0\\.\\d*[1-9]\\d*)|([1-9]\\d*)",String.valueOf(x)))
                    return x;
                else if(Pattern.matches("-([1-9]\\d*.\\d*|0\\.\\d*[1-9]\\d*)|(-[1-9]\\d*)",String.valueOf(x)))
                    return x*-1;
            }
            default:
                text.setFont(new Font("Comic Sans Ms",Font.BOLD+Font.ITALIC,40));
                return y;
        }
    }

    //历史记录弹出
    public void showDialog(){
        dialog=new JDialog(this,"历史记录",true);
        dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
        dialog.setSize(750,500);
        dialog.setLocationRelativeTo(null);
        dialog.add(jsHistory);
        dialog.setVisible(true);
    }

}

